package com.mychessproject;

import java.util.ArrayList;
import java.util.Random;

import android.util.Log;


public class Chess {

	//Log Tag String
	final static String TAG = "WeAndroids";

	//chess pieces name
	final static String[] chess_pieces_names={"","BRook","BKnight","BBishop","BQueen","BKing","BPawn","WRook","WKnight","WBishop","WQueen","WKing","WPawn"};
	
	//8x8 chess pieces, the value of each element is the position number of chess_pieces_names
	private int[][] chess_pieces_positions;
	
	//0 no castling
	//1 white king short  castling
	//2 white king long  castling
	//3 black king short  castling
	//4 black king long  castling
	private int castling_status;
	
	//0 no in check status
	//1 white king in check status
	//2 black king in check status	
	private int king_in_check_status;
	
	//0 white chess turn
	//1 black chess turn	
	private int chess_turn_status;
	
	//1 It is white chess turn(you move the black turn)
	//2 it is black chess turn(you move the white turn)	
	//0 No error
	private int chess_turn_error;
	
	//0 no pawn moves to the last line
	//1 white pawn moves to the last line (0 line)
	//2 black pawn moves to the last line (7 line)	
	private int pawn_last_row_status;
	
	//The pawn that taking en-passant (move a double step)
	private int pawn_double_stepsx;
	private int pawn_double_stepsy;
	
	//randomly move positions
	private int random_move_from;
	private int random_move_to;
	
	//Construct function
	public Chess()
	{
		chess_pieces_positions=new int[8][8];
	}

	//initialization
	public void init()
	{
		castling_status=0;
		king_in_check_status=0;
		chess_turn_status=0;
		pawn_last_row_status=0;
		pawn_double_stepsx=0;
		pawn_double_stepsy=0;
		
		//Black chess pieces
		chess_pieces_positions[0][0]=1;
		chess_pieces_positions[0][1]=2;
		chess_pieces_positions[0][2]=3;
		chess_pieces_positions[0][3]=4;
		chess_pieces_positions[0][4]=5;
		chess_pieces_positions[0][5]=3;
		chess_pieces_positions[0][6]=2;
		chess_pieces_positions[0][7]=1;
		
		chess_pieces_positions[1][0]=6;
		chess_pieces_positions[1][1]=6;
		chess_pieces_positions[1][2]=6;
		chess_pieces_positions[1][3]=6;
		chess_pieces_positions[1][4]=6;
		chess_pieces_positions[1][5]=6;
		chess_pieces_positions[1][6]=6;
		chess_pieces_positions[1][7]=6;
		
		//Blank chess pieces
		for(int i=2;i<6;i++)
		{
			for(int j=0;j<8;j++)
			{
				chess_pieces_positions[i][j]=0;
			}
		}

		//White chess pieces
		chess_pieces_positions[7][0]=7;
		chess_pieces_positions[7][1]=8;
		chess_pieces_positions[7][2]=9;
		chess_pieces_positions[7][3]=10;
		chess_pieces_positions[7][4]=11;
		chess_pieces_positions[7][5]=9;
		chess_pieces_positions[7][6]=8;
		chess_pieces_positions[7][7]=7;
		
		chess_pieces_positions[6][0]=12;
		chess_pieces_positions[6][1]=12;
		chess_pieces_positions[6][2]=12;
		chess_pieces_positions[6][3]=12;
		chess_pieces_positions[6][4]=12;
		chess_pieces_positions[6][5]=12;
		chess_pieces_positions[6][6]=12;
		chess_pieces_positions[6][7]=12;
	}
	
	//check the chess piece if it is ok to move from  frompos to topos
	//king_in_check =0 
	//random_check =1 use only in function random_move()
	public boolean chess_rules_check(int frompos, int topos,int random_check)
	{
		boolean result_status=true;
		int fromposx=frompos%8;
		int fromposy=frompos/8;
		
		int toposx=topos%8;
		int toposy=topos/8;
		
		chess_turn_error=0;
		
		//white chess turn, but move black chess 
		if(random_check==0&chess_turn_status==0&chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].startsWith("B")) // White Turn
		{
			chess_turn_error=1;
			result_status=false;
		}
		
		//black chess turn, but move white chess		
		if(random_check==0&chess_turn_status==1&chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].startsWith("W")) // Black Turn
		{
			chess_turn_error=2;
			result_status=false;
		}
		
	    //The rook moves in a straight line, horizontally or vertically
		//The rook may not jump over other pieces.
		if(chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].contains("Rook"))
		{
			int move_line_space=Math.abs(toposy-fromposy);
			int move_column_space=Math.abs(toposx-fromposx);
			
			if(move_line_space==0||move_column_space==0)
			{
				if(move_line_space==0)   //move in the same line
				{
					int movefrom=fromposx;
					int moveto=toposx;
					if(fromposx>toposx)
					{
						movefrom=toposx;
						moveto=fromposx;
					}
					
					for(int i=movefrom+1;i<moveto;i++)
					{
						if(chess_pieces_positions[fromposy][i]!=0)  // there is a chess piece between the columns of movefrom and moveto
						{
							result_status=false;
						}
					}
				}
				
				if(move_column_space==0)  //move in the same column
				{
					int movefrom=fromposy;
					int moveto=toposy;
					if(fromposy>toposy)
					{
						movefrom=toposy;
						moveto=fromposy;
					}
					
					for(int i=movefrom+1;i<moveto;i++)
					{
						if(chess_pieces_positions[i][fromposx]!=0) // there is a chess piece between the lines of movefrom and moveto
						{
							result_status=false;
						}
					}
				}

			}
			else
				result_status=false;
		}
		
		
		//The bishop moves in a straight diagonal line.
		//The bishop may also not jump over other pieces.
		if(chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].contains("Bishop"))
		{
			int move_line_space=toposy-fromposy;
			int move_column_space=toposx-fromposx;
			
			if(Math.abs(move_line_space)==Math.abs(move_column_space))  //the lines space diff must equal to columns diff space
			{
				for(int i=1;i<Math.abs(move_line_space);i++)
				{
					//check if there is a not blank chess piece in the straight diagonal line
					if(chess_pieces_positions[fromposy+move_line_space/Math.abs(move_line_space)*i][fromposx+move_column_space/Math.abs(move_line_space)*i]!=0)
					{
						result_status=false;
					}
				}
				
			}
			else
				result_status=false;
		}
		
		//The queen has the combined moves of the rook and the bishop (in any straight line, horizontal, vertical, or diagonal)
		//moves in a straight line, horizontally or vertically
		//moves in a straight diagonal line
		//may also not jump over other pieces.
		if(chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].contains("Queen"))
		{
			int move_line_space=toposy-fromposy;
			int move_column_space=toposx-fromposx;
			

			if(Math.abs(move_line_space)==Math.abs(move_column_space)||move_line_space==0||move_column_space==0)
			{
				
				if(Math.abs(move_line_space)==Math.abs(move_column_space))  // moves like a bishop
				for(int i=1;i<Math.abs(move_line_space);i++)
				{
					if(chess_pieces_positions[fromposy+move_line_space/Math.abs(move_line_space)*i][fromposx+move_column_space/Math.abs(move_line_space)*i]!=0)
					{
						result_status=false;
					}
				}
				
				
				if(move_line_space==0)   // moves like a rook - same line
				{
					int movefrom=fromposx;
					int moveto=toposx;
					if(fromposx>toposx)
					{
						movefrom=toposx;
						moveto=fromposx;
					}
					
					for(int i=movefrom+1;i<moveto;i++)
					{
						if(chess_pieces_positions[fromposy][i]!=0)
						{
							result_status=false;
						}
					}
				}
				
				if(move_column_space==0)  // moves like a rook - same column
				{
					int movefrom=fromposy;
					int moveto=toposy;
					if(fromposy>toposy)
					{
						movefrom=toposy;
						moveto=fromposy;
					}
					
					for(int i=movefrom+1;i<moveto;i++)
					{
						if(chess_pieces_positions[i][fromposx]!=0)
						{
							result_status=false;
						}
					}
				}
			}
			else
				result_status=false;
		}
		
		//The knight makes a move that consists of first one step in a horizontal or vertical direction, 
		//and then one step diagonally in an outward direction.
		//The knight jumps
		if(chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].contains("Knight"))
		{
			int move_line_space=Math.abs(toposy-fromposy);    // the line spaces diff
			int move_column_space=Math.abs(toposx-fromposx);  // the column spaces diff
			
			if((move_line_space==1 & move_column_space==2) || (move_line_space==2 & move_column_space==1 ))
			{
				;
			}
			else
				result_status=false;
		}
		
		//Pawn can not go back
		//Pawn can move one step(square) straight forward
		//Pawn can move two/double steps straight forward from the the second row (from the player size)
		//When taking, the pawn goes one square diagonally forward.
		//When taking en-passant , When a pawn makes a double step from the second row to the fourth row, and there is an enemy pawn on an adjacent square on the fourth row (from the player view), then this enemy pawn inthe next move may move diagonally to the square that was passed over by the double-stepping pawn, which is on the third row. In this same move, the double-stepping pawn is taken.
		//This taking en-passant must be done directly
		//Not jump over other pieces.
		
		if(chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].contains("Pawn"))
		{
			int move_line_space=toposy-fromposy;
			int move_column_space=toposx-fromposx;
			
			if(chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].contains("BPawn"))
			{
				//move straight forward one step or two steps (two steps only from the second row)
				//move one square diagonally forward to take the opponent chess
				//move one square diagonally forward and its adjacent square is a opponent pawn with a double step from the second row
				if((move_column_space==0&(move_line_space==1||move_line_space==2))||(move_column_space==-1&move_line_space==1)||((move_column_space==1&move_line_space==1)&(pawn_double_stepsx==toposx&pawn_double_stepsy==toposy-1)))
				{
					if(move_column_space==0)  // move straight forward
					{
						if(chess_pieces_positions[toposy][toposx]!=0)  // the topos chess piece is not blank
						{
							result_status=false;
						}
					}
					
					if(move_column_space==-1&move_line_space==1)  //move one square diagonally forward to take
					{
						if(chess_pieces_positions[toposy][toposx]==0)
						{
							if(pawn_double_stepsx==toposx&pawn_double_stepsy==toposy-1)  //if there a adjacent square with a double step from the second row
								;
							else                    //There is not opponent chess to be taken 
								result_status=false;
						}
					}
					
					if(move_line_space==2)   // move straight forward 2 steps
					{
						if(fromposy!=1||chess_pieces_positions[fromposy+1][fromposx]!=0)  // not from the second row or there is a no-blank chess piece in front of it
						{
							result_status=false;
						}
						else
						{
							//from the second row
							//topos chess piece is blank 
							if(chess_pieces_positions[toposy][toposx]==0)  // || !chess_pieces_names[chess_pieces_positions[toposy][toposx]].substring(0, 1).equals(chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].substring(0, 1)))
							{
								pawn_double_stepsx=toposx;
								pawn_double_stepsy=toposy;
							}
						}
					}
					
					//Black pawn move to the last row
					//Board promote happens
					if(result_status&toposy==7&random_check==0)
					{
						pawn_last_row_status=2;
					}
				}
				else
					result_status=false;
			}
			
			if(chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].contains("WPawn"))
			{
				//move straight forward one step or two steps (two steps only from the second row)
				//move one square diagonally forward to take the opponent chess
				//move one square diagonally forward and its adjacent square is a opponent pawn with a double step from the second row
				if((move_column_space==0&(move_line_space==-1||move_line_space==-2))||(move_column_space==1&move_line_space==-1)||((move_column_space==-1&move_line_space==-1)&(pawn_double_stepsx==toposx&pawn_double_stepsy==toposy+1)))
				{
					if(move_column_space==0) // move straight forward
					{
						if(chess_pieces_positions[toposy][toposx]!=0)  // the topos chess piece is not blank
						{
							result_status=false;
						}
					}
					
					if(move_column_space==1&move_line_space==-1)   //move one square diagonally forward to take
					{
						if(chess_pieces_positions[toposy][toposx]==0)
						{
							if(pawn_double_stepsx==toposx&pawn_double_stepsy==toposy+1)  //if there a adjacent square with a double step from the second row
								;
							else   //There is not opponent chess to be taken 
								result_status=false;
						}
					}
					
					if(move_line_space==-2)  // move straight forward 2 steps
					{
						 // not from the second row (from the player view) or there is a no-blank chess piece in front of it
						if(fromposy!=6||chess_pieces_positions[fromposy-1][fromposx]!=0)
						{
							result_status=false;
						}
						else
						{
							//from the second row
							//topos chess piece is blank 
							if(chess_pieces_positions[toposy][toposx]==0)// || !chess_pieces_names[chess_pieces_positions[toposy][toposx]].substring(0, 1).equals(chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].substring(0, 1)))
							{
								pawn_double_stepsx=toposx;
								pawn_double_stepsy=toposy;
							}
						}
					}
					
					//White pawn move to the last row
					//Board promote happens					
					if(result_status&toposy==0&random_check==0)
					{
						pawn_last_row_status=1;
					}
				}
				else
					result_status=false;
			}
		}
		
		
		//The king moves one square in any direction, horizontally, vertically, or diagonally.
		//Castling --
		//  the king moves two squares towards the rook, and the rook moves over the king to the next square
		//  the King and rook must occupy the same rank
		//  all squares between the rook and king are blank.
		
		if(chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].contains("King"))
		{
			int move_line_space=Math.abs(toposy-fromposy);
			int move_column_space=Math.abs(toposx-fromposx);
			
			//move one square in any direction, horizontally, vertically, or diagonally
			if((move_line_space==0&move_column_space==1)||(move_line_space==1&move_column_space==0)||(move_line_space==1&move_column_space==1))
			{
				;
			}
			else //Castling
			{
				// the king moves two squares at the same row
				if(move_column_space==2&move_line_space==0&fromposx==4) 
				{
					// White King
					if(chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].contains("WKing"))  
					{
						if(toposx>fromposx)   // short move
						{
							//all squares between the rook and king are blank
							//there is a white rook in the same line
							if(chess_pieces_positions[fromposy][fromposx+1]==0&chess_pieces_positions[fromposy][fromposx+2]==0&chess_pieces_names[chess_pieces_positions[fromposy][fromposx+3]].contains("WRook"))
							{
								castling_status=1;
							}
							else
								result_status=false;
						}
						else                 // long move
						{
							//all squares between the rook and king are blank
							//there is a white rook in the same line
							if(chess_pieces_positions[fromposy][fromposx-1]==0&chess_pieces_positions[fromposy][fromposx-2]==0&chess_pieces_positions[fromposy][fromposx-3]==0&chess_pieces_names[chess_pieces_positions[fromposy][fromposx-4]].contains("WRook"))
							  {
								  castling_status=2;
							  }
							  else
								  result_status=false;
						}
					}
					
					// Black King
					if(chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].contains("BKing"))
					{
						if(toposx>fromposx)  // short move
						{
							
							//all squares between the rook and king are blank
							//there is a white rook in the same line
							if(chess_pieces_positions[fromposy][fromposx+1]==0&chess_pieces_positions[fromposy][fromposx+2]==0&chess_pieces_names[chess_pieces_positions[fromposy][fromposx+3]].contains("BRook"))
							{
								castling_status=3;
							}
							else
								result_status=false;
						}
						else                  // long move
						{
							//all squares between the rook and king are blank
							//there is a white rook in the same line
							if(chess_pieces_positions[fromposy][fromposx-1]==0&chess_pieces_positions[fromposy][fromposx-2]==0&chess_pieces_positions[fromposy][fromposx-3]==0&chess_pieces_names[chess_pieces_positions[fromposy][fromposx-4]].contains("BRook"))
							  {
								  castling_status=4;
							  }
							  else
								  result_status=false;
						}
					}
				}
				else
					result_status=false;
			}
		}
		
		//the topos chess piece is not blank 
		//the topos chess piece with the same color
		if(chess_pieces_positions[toposy][toposx]!=0 )
		if(chess_pieces_names[chess_pieces_positions[toposy][toposx]].substring(0, 1).equals(chess_pieces_names[chess_pieces_positions[fromposy][fromposx]].substring(0, 1)))
		{
			result_status=false;
		}
		
		return result_status;
	}
	
	
	//move the chess piece from frompos to topos
	//used when the chess_rules_check return true
	public int move_chess_pieces(int frompos, int topos)
	{
		int fromposx=frompos%8;
		int fromposy=frompos/8;
		
		int toposx=topos%8;
		int toposy=topos/8;
		
		int taken=0;
		
		//white and black turns switch
		if(chess_turn_status==0)
			chess_turn_status=1;
		else
			chess_turn_status=0;
		
		//if the topos is not blank, and taking happen
		if(chess_pieces_positions[toposy][toposx]!=0)
			taken=1;
		
		//update the topos chess piece to the frompos chess piece
		//update the from chess piece to blank
		chess_pieces_positions[toposy][toposx]=chess_pieces_positions[fromposy][fromposx];
		chess_pieces_positions[fromposy][fromposx]=0;
		
		//white king short move
		//move the white rook to the left of the king
		if(castling_status==1)
		{
			chess_pieces_positions[toposy][toposx-1]=7;
			chess_pieces_positions[toposy][toposx+1]=0;
		}
		
		//white king long move
		//move the white rook to the right of the king
		if(castling_status==2)
		{
			chess_pieces_positions[toposy][toposx+1]=7;
			chess_pieces_positions[toposy][toposx-2]=0;
		}
		
		//black king short move
		//move the black rook to the left of the king
		if(castling_status==3)
		{
			chess_pieces_positions[toposy][toposx-1]=1;
			chess_pieces_positions[toposy][toposx+1]=0;
		}
		
		//black king long move
		//move the black rook to the right of the king
		if(castling_status==4)
		{
			chess_pieces_positions[toposy][toposx+1]=1;
			chess_pieces_positions[toposy][toposx-2]=0;
		}
		
		castling_status=0;

		//taking en-passant
		//A pawn makes a double step
		if(pawn_double_stepsx>0||pawn_double_stepsy>0)
		{
			//if you move a black pawn and there is a white pawn in front of your black pawn (from the play view)
			if(chess_pieces_names[chess_pieces_positions[toposy][toposx]].startsWith("BPawn"))
			if(chess_pieces_names[chess_pieces_positions[toposy-1][toposx]].contains("WPawn"))	
			{
				// the white pawn just makes a double step 
				if(pawn_double_stepsy==toposy-1 & toposx==pawn_double_stepsx)
				{
					// the white pawn is taken. 
					chess_pieces_positions[toposy-1][toposx]=0;
					taken=1;
				}
			}
			
			//if you move a white pawn and there is a black pawn in front of your black pawn (from the play view)
			if(chess_pieces_names[chess_pieces_positions[toposy][toposx]].startsWith("WPawn"))
			if(chess_pieces_names[chess_pieces_positions[toposy+1][toposx]].contains("BPawn"))
			{
				// the black pawn just makes a double step 
				if(pawn_double_stepsy==toposy+1 & toposx==pawn_double_stepsx)
				{
					// the black pawn is taken. 
					chess_pieces_positions[toposy+1][toposx]=0;
					taken=1;
				}
			}
			
			// any chess moves, taking en-passant is canceled
			// except the chess (just makes a double step) 
			if(toposy!=pawn_double_stepsy&toposx!=pawn_double_stepsx)
			{
				pawn_double_stepsx=0;
				pawn_double_stepsy=0;
			}
		}
		
		//if the pawn moves to the last row
		if(pawn_last_row_status!=0)
		{
			//but there is a winner
			if(winning_status()!=0)
				pawn_last_row_status=0;
		}
		
		return taken;
	}
	
	//check if the king is in check status
	public int king_in_check_status()
	{
		king_in_check_status=0;
		
		//white king position
		int whitekingpos=-1;

		//black king position
		int blackkingpos=-1;
		for(int i=0;i<8;i++)
			for(int j=0;j<8;j++)
			{
				if(chess_pieces_names[chess_pieces_positions[i][j]].contains("WKing"))
				{
					//white king position
					whitekingpos=i*8+j;
				}
			}
		

		if(whitekingpos!=-1)
		{
			for(int i=0;i<8;i++)
				for(int j=0;j<8;j++)
				{
					if(chess_pieces_names[chess_pieces_positions[i][j]].startsWith("B"))
					{
						boolean check_status= chess_rules_check(i*8+j,whitekingpos,1);
						if(check_status)
						{
							//white king in check status
							king_in_check_status=1;
						}
					}
				}
		}
		
		
		for(int i=0;i<8;i++)
			for(int j=0;j<8;j++)
			{
				if(chess_pieces_names[chess_pieces_positions[i][j]].contains("BKing"))
				{
					//black king position					
					blackkingpos=i*8+j;
				}
			}
		
		
		if(blackkingpos!=-1)
		{
			for(int i=0;i<8;i++)
				for(int j=0;j<8;j++)
				{
					if(chess_pieces_names[chess_pieces_positions[i][j]].startsWith("W"))
					{
						boolean check_status= chess_rules_check(i*8+j,blackkingpos,1);
						if(check_status)
						{
							//black king in check status							
							king_in_check_status=2;
						}
					}
				}
		}
		
		return king_in_check_status;
	}
	
	public int getchess_turn_error()
	{
		return this.chess_turn_error;
	}
	
    
	public void setchess_pieces_positions(int posx, int posy, int chess_pos)
	{
		this.chess_pieces_positions[posy][posx]=chess_pos;
	}
	
	public int getpawn_last_row_status()
	{
		return this.pawn_last_row_status;
	}
	
	public void setpawn_last_row_status(int status)
	{
		this.pawn_last_row_status=status;
	}
	
	public int getchess_pieces_positions(int pos)
	{
		return this.chess_pieces_positions[pos/8][pos%8];
	}
	
	//check who wins
	public int winning_status()
	{
		int white_king_exist=0;
		int black_king_exist=0;
		int winning_status=0;
		
		for(int i=0;i<8;i++)
			for(int j=0;j<8;j++)
			{
				if(chess_pieces_names[chess_pieces_positions[i][j]].equals("WKing"))
				{
					white_king_exist=1;
					break;
				}
				
				if(chess_pieces_names[chess_pieces_positions[i][j]].equals("BKing"))
				{
					black_king_exist=1;
					break;
				}
				
			}
		
		//white king exist
		//black king not exist
		if(white_king_exist==1 & black_king_exist==0)
			winning_status= 1;
		
		//black king exist
		//white king not exist		
		if(white_king_exist==0 & black_king_exist==1)
			winning_status= 2;
		
		 Log.d(TAG, "winning_status");
		return winning_status;
		
	}
	
	public int getchess_turn_status()
	{
		return this.chess_turn_status;
	}
	
	//randomly move a white or black chess piece 
	public boolean random_move()
	{
		boolean movable = false;
		Random from_random;
		Random to_random;
		
		if(chess_turn_status==0)  //White chess turn
		{
			  ArrayList<Integer> from_chess_pieces; 
			  ArrayList<Integer> to_chess_pieces;
			  ArrayList<Integer> to_chess_pieces_temp;
			  
			  from_chess_pieces = new ArrayList<Integer>();
			  to_chess_pieces= new ArrayList<Integer>();
			  
			  for(int i=0;i<8;i++)
				  for(int j=0;j<8;j++)
				  {
					  if(chess_pieces_names[chess_pieces_positions[i][j]].startsWith("W"))
					  {
						  from_chess_pieces.add(i*8+j);  //add all the white chess pieces positions
					  }
					  if(!chess_pieces_names[chess_pieces_positions[i][j]].startsWith("W"))
					  {
						  to_chess_pieces.add(i*8+j);    //add all the non-white chess pieces (include the blank) positions
					  }
				  }
			  
			  do{
				  from_random=new Random(System.currentTimeMillis()+from_chess_pieces.size());  //System.currentTimeMillis()+from_chess_pieces.size() to be the seed
				  int from_chess_no = from_random.nextInt(from_chess_pieces.size());   //select a random chess from the array from_chess_pieces
				  to_chess_pieces_temp= new ArrayList<Integer>();
				  for(int i=0;i<to_chess_pieces.size();i++)
					  to_chess_pieces_temp.add(to_chess_pieces.get(i));  
				  
				  for(int i=0;i<to_chess_pieces_temp.size();i++)
				  {
					  random_move_from = from_chess_pieces.get(from_chess_no);    // get random_move_from position from the white chess pieces array
					  
					  to_random=new Random(System.currentTimeMillis()+to_chess_pieces_temp.size());
					  int to_chess_no = to_random.nextInt(to_chess_pieces_temp.size());   //select a random chess from the non-white chess pieces array
					  random_move_to = to_chess_pieces_temp.get(to_chess_no);     // get random_move_to from the non-white chess pieces array
					  
					  movable=chess_rules_check(random_move_from,random_move_to,1);  //check the chess rules 
					  if(movable)
						  break;
					  
					  to_chess_pieces_temp.remove(to_chess_no);    // remove to_chess_no from the non-white chess pieces array
				  }
				  from_chess_pieces.remove(from_chess_no);  // remove from_chess_no from the white chess pieces array
			  }while(!movable);
		}
		
		if(chess_turn_status==1)  //Black chess turn
		{
			  
			  ArrayList<Integer> from_chess_pieces;
			  ArrayList<Integer> to_chess_pieces;
			  ArrayList<Integer> to_chess_pieces_temp;
			  
			  from_chess_pieces = new ArrayList<Integer>();
			  to_chess_pieces= new ArrayList<Integer>();
			  
			  
			  for(int i=0;i<8;i++)
				  for(int j=0;j<8;j++)
				  {
					  if(chess_pieces_names[chess_pieces_positions[i][j]].startsWith("B"))
					  {
						  from_chess_pieces.add(i*8+j);  //add all the black chess pieces positions
					  }
					  if(!chess_pieces_names[chess_pieces_positions[i][j]].startsWith("B"))
					  {
						  to_chess_pieces.add(i*8+j);   //add all the non-black chess pieces (include the blank) positions
					  }
				  }
			  
			  do{
				  from_random=new Random(System.currentTimeMillis()+from_chess_pieces.size());
				  int from_chess_no = from_random.nextInt(from_chess_pieces.size());  //select a random chess from the array from_chess_pieces
				  to_chess_pieces_temp= new ArrayList<Integer>();
				  for(int i=0;i<to_chess_pieces.size();i++)
					  to_chess_pieces_temp.add(to_chess_pieces.get(i));
				  
				  for(int i=0;i<to_chess_pieces_temp.size();i++)
				  {
					  random_move_from = from_chess_pieces.get(from_chess_no);  // get random_move_from position from the black chess pieces array
					  
					  to_random=new Random(System.currentTimeMillis()+to_chess_pieces_temp.size()); 
					  int to_chess_no = to_random.nextInt(to_chess_pieces_temp.size()); //select a random chess from the non-white chess pieces array
					  random_move_to = to_chess_pieces_temp.get(to_chess_no);  // get random_move_to from the non-white chess pieces array
					  
					  movable=chess_rules_check(random_move_from,random_move_to,1);   //check the chess rules 
					  if(movable)
						  break;
					  
					  to_chess_pieces_temp.remove(to_chess_no);  // remove to_chess_no from the non-black chess pieces array
				  }
				  from_chess_pieces.remove(from_chess_no);  // remove from_chess_no from the black chess pieces array
			  }while(!movable);
		}
		
		return movable;
	}
	
	public int getrandom_move_from()
	{
		return this.random_move_from;
	}
	
	public int getrandom_move_to()
	{
		return this.random_move_to;
	}
	
}
